package com.cnpinxi.service;

import com.cnpinxi.domain.Order;
import com.cnpinxi.myRunTime.MyRunTimeExcption;
import com.cnpinxi.service.*;
import com.cnpinxi.service.OrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Baggio on 2017-11-22.
 */
@Service
public class RefundServiceImpl  implements com.cnpinxi.service.RefundService {
@Autowired
com.cnpinxi.service.OrderService orderService;
    @Override
    public Boolean refund(Long orderId, String refundReason) {
        Order order=orderService.getOrderById(orderId);
        if(order==null){
            throw new MyRunTimeExcption("订单不存在！");
        }
        Byte status=order.getStatus();
        if(status!= com.cnpinxi.service.OrderService.PAYED && status!= com.cnpinxi.service.OrderService.WAIT_DISPATCH && status!= com.cnpinxi.service.OrderService.DISPATCHED){
            throw new MyRunTimeExcption("已经不能申请退款！");
        }
        if(StringUtils.isNotBlank(refundReason)){
            order.setRefundReason(refundReason);
        }
        order.setStatus(com.cnpinxi.service.OrderService.REFUND);
        orderService.modifyOrder(order);
        return true;
    }

    @Override
    public Boolean cancelRefund(Long orderId) {
        Order order=orderService.getOrderById(orderId);



        if(order.getStatus()!= com.cnpinxi.service.OrderService.REFUND){
            throw new MyRunTimeExcption("订单状态不能取消退款！");
        }

        orderService.modifyOrderStatus(orderId, com.cnpinxi.service.OrderService.CANCEL_REFUND);
        return true;
    }

    @Override
    public Boolean refundConfirm(Long orderId) {
        Order order=orderService.getOrderById(orderId);

        if(order.getStatus()!= com.cnpinxi.service.OrderService.REFUND){
            throw new MyRunTimeExcption("订单状态不能确认退款！");
        }

        orderService.modifyOrderStatus(orderId, OrderService.REFUNDED);
        return true;
    }
}
